package ios.di.logviewer.data;

import java.io.File;
import java.util.*;
/**
 * 日志文件装载器，将指定目录下的所有日志文件进行过滤（用到了LogFileFilter）、排序，返回符合条件的文件
 * @author LuoHouQi
 *
 */
public class LogFileLoader {
	String logDir;
	LogFileFilter fileFilter;
	Date happenDateStart;
	Date happenDateEnd;
	private File[] logFiles;
	
	
	
	public LogFileLoader(String logDir, Date happenDateStart,
			Date happenDateEnd) {
		super();
		this.logDir = logDir;
		this.happenDateStart = happenDateStart;
		this.happenDateEnd = happenDateEnd;
		
		this.fileFilter = new LogFileFilter();
		this.fileFilter.setHappenDateStart(happenDateStart);
		this.fileFilter.setHappenDateEnd(happenDateEnd);
	    File dir = new File(logDir);
	    
	    if(dir.isFile())
	    {
	    	logFiles = new File[1];
	    	logFiles[0]= dir; 
	    }
	    else
	    {
	    	logFiles = dir.listFiles(fileFilter);
	    	this.sortFiles();
	    }	    
	}



	



	public File[] getLogFiles()
	{
		return this.logFiles;
	}
	/**
	 * 按文件名进行排序
	 */
	private void  sortFiles()
	{
		Arrays.sort(this.logFiles, new Comparator<File>()
		{

			@Override
			public int compare(File file1, File file2) {
				
				return file1.getName().compareTo(file2.getName());
			}
			
			
		});
	}
}
